package com.mage.servlet;

import com.mage.bean.User;
import com.mage.dao.UserDao;
import com.mage.dao.impl.UserDaoImpl;
import sun.plugin2.message.Message;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/UserBaofangServlet")
public class UserBaofangServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    public UserBaofangServlet() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            request.setCharacterEncoding("UTF-8");
            //1. 获取客户端提交上来的数据
            int userid = Integer.parseInt(request.getParameter("userid"));
            //2. 添加到数据库
            UserDao service = new UserDaoImpl();
            User user = service.findUserById(userid);
            if (user.getWxf() < 10.00){
                request.getRequestDispatcher("/Super/KaijiError.jsp").forward(request, response);
            }else{
                user.setYxf(user.getYxf() + 10.0);
                user.setWxf(user.getWxf()-10.0);
                service.update(user);
                request.getRequestDispatcher("/Super/KaijiSuccess.jsp").forward(request, response);
            }



        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
